/**
 * Created with JetBrains WebStorm.
 * Date: 17/11/20
 * Time: 下午1:51
 * Desc: 新闻列表.
 */
import NewsTpl from './news.html';
import _ from 'underscore';
import Util from '../../../common-component/util/util.js';
import Table from '../../../common-component/table/table.js';
import API from '../../../api/Api.js';
import Dialog from '../../../common-component/dialog/dialog.js'
import commonModal from '../../service-common/service-common.js';
import Pagination from '../../../common-component/pagination/pagination.js';
import ChoiceDialog from '../../../common-component/choice-dialog/choice-dialog.js';

import "./news.scss";

export default function DotInformation() {

    $("#app-container").html( NewsTpl({name: '新闻模块'}) );

    const handlers = {
        checkedList: null,
        isAjax: false,
        querySearch: null,
        params: {
            pageNO: 1,
            pageSize: 10
        },
        init: function(evt) {
            const _this = this;
            this.queryForm().bindEvent();
            this.querySearch = Pagination('#pagination', {
                pageSize: 10,
                url: API.newsList,
                data: _this.params,
                type: 'post',
                changeParam: function(setParam, pageNO, pageSize) {
                    _this.params.pageNO = pageNO;
                    _this.params.pageSize = pageSize;
                    setParam(_this.params);
                },
                success: function(json) {
                    let tHeader = _this.getHeader(json.rows);
                    _this.checkedList = Table($(".news-table"), 'news-table', {
                        tHeader: tHeader,
                        data: json.rows,
                        opFunction: (obj) => {
                            _this.FormButton(obj);
                        }
                    },() => {
                        $(".news-table").colResizable({
                            minWidth: 100,
                            selfResize: true
                        });
                    });
                },
                error: function() {

                },
                complete: function() {

                }
            })
        },

       getHeader: function(pageData){
            let _this = this;
            return [{title: '标题', width: '120px', name: 'title', handler: function(str){
                        return str;
                    }}
                    , {title: '访问量', name:  'PV', handler: function(str){
                        return str;
                    }}
                    , {title: '更新日期', name:  'updateDate', handler: function(str){
                        return str;
                    }}
                    ,{title: '是否推荐', name:  'recommend', handler: function(str){
                        return str == 1 ? '是' : '否';
                    }}
                    ,{title: '是否置顶', name:  'stick', handler: function(str){
                        return str == 1 ? '是' : '否';
                    }}
                    ,{title: '状态', name: 'shelf', handler: function(str){
                        return str == 1 ? '上架' : '下架';
                    }}
                    ,
                    //{title: '排序', name:  'index', handler: function(str){
                    //    return str;
                    //}},
                    {title: '操作', width: '100px', hasFunc: true ,
                    funcList:[{name:'编辑', op: 'update', cls: 'news-update'},
                    {name: '删除', op: 'delete', cls: 'news-del'},
                    {name:'推荐', op: 'review', cls: 'news-Rec'},
                    {name:'置顶', op: 'start', cls: 'news-Top'},
                    {name:'上架', op: 'bottom', cls: 'news-Shelf'},
                    {name:'下架', op: 'stop', cls: 'news-ShelfStop'}
                    ],handler: function(data, obj, index){
                        var filterList = [{name:'编辑', op: 'update', cls: 'news-update'},{name: '删除', op: 'delete', cls: 'news-del'}]
                        if(obj.recommend == 0){
                            filterList.push({name:'推荐', op: 'review', cls: 'news-Rec'})
                        }else if(obj.stick == 0){
                            filterList.push({name:'置顶', op: 'start', cls: 'news-Top'})
                        }else if(obj.shelf == 0){
                            filterList.push({name:'上架', op: 'bottom', cls: 'news-Shelf'})
                        }else if(obj.shelf == 1){
                            filterList.push({name:'下架', op: 'stop', cls: 'news-ShelfStop'})
                        }
                        return filterList;
                    }}
            ];
        },
        /*
         * 表单按钮操作
         * */
        FormButton: function (e) {
            const _this = this;
            switch(e.op)
            {
                case 'delete'://delete
                    new Dialog({title:'删除',content:'确认删除吗？',button:['确定','取消'],width:500,
                        noClose:true,
                        confirm:function(evt){
                            evt.remove();
                            $.ajax({
                                url: API.newsDelById,
                                type: 'get',
                                data:{id:e.obj.id},
                                success(data) {
                                    if(data.success){
                                        Util.alertMessage("操作成功",function () {
                                            _this.querySearch(_this.params);
                                        },2000)
                                    }else{
                                        Util.alertMessage(data.error)
                                    }
                                }
                            });
                        }});
                    break;
                case 'review'://推荐
                    new Dialog({title:'推荐',content:'确认推荐吗？',button:['确定','取消'],width:500,
                        noClose:true,
                        confirm:function(evt){
                            evt.remove();
                            $.ajax({
                                url: API.newsRecommendState,
                                type: 'get',
                                data:{id:e.obj.id,state:1},
                                success(data) {
                                    if(data.success){
                                        Util.alertMessage("操作成功",function () {
                                            _this.querySearch(_this.params);
                                        },2000)
                                    }else{
                                        Util.alertMessage(data.error)
                                    }
                                }
                            });
                        }});
                    break;
                case 'stop'://下架
                    new Dialog({title:'下架',content:'确认下架吗？',button:['确定','取消'],width:500,
                        noClose:true,
                        confirm:function(evt){
                            evt.remove();
                            $.ajax({
                                url: API.newsShelfState,
                                type: 'get',
                                data:{id:e.obj.id,state:0},
                                success(data) {
                                    if(data.success){
                                        Util.alertMessage("操作成功",function () {
                                            _this.querySearch(_this.params);
                                        },2000)
                                    }else{
                                        Util.alertMessage(data.error)
                                    }
                                }
                            });
                        }});
                    break;
                case 'bottom'://上架
                    new Dialog({title:'上架',content:'确认上架吗？',button:['确定','取消'],width:500,
                        noClose:true,
                        confirm:function(evt){
                            evt.remove();
                            $.ajax({
                                url: API.newsShelfState,
                                type: 'get',
                                data:{id:e.obj.id,state:1},
                                success(data) {
                                    if(data.success){
                                        Util.alertMessage("操作成功",function () {
                                            _this.querySearch(_this.params);
                                        },2000)
                                    }else{
                                        Util.alertMessage(data.error)
                                    }
                                }
                            });
                        }});
                    break;
                case 'start'://置顶
                    new Dialog({title:'置顶',content:'确认置顶吗？',button:['确定','取消'],width:500,
                        noClose:true,
                        confirm:function(evt){
                            evt.remove();
                            $.ajax({
                                url: API.newsStickState,
                                type: 'get',
                                data:{id:e.obj.id,state:1},
                                success(data) {
                                    if(data.success){
                                        Util.alertMessage("操作成功",function () {
                                            _this.querySearch(_this.params);
                                        },2000)
                                    }else{
                                        Util.alertMessage(data.error)
                                    }
                                }
                            });
                        }});
                    break;
                case 'update'://修改
                    window.location.href = '#/news-update?id='+e.obj.id;
                    break;
            }
        },
        /**
         * 绑定事件
         */
        bindEvent: function() {
            const _this = this;
            $("body").on('click', '.js-handle', function(e) {
                const $this = $(this), handle = $this.data('handle');
                _this[handle] && _this[handle]($this, e);
            })
                .on('click', '.main-data', function() {
                    const $this = $(this), idx = $this.index();
                    if(_this.isAjax) {
                        return;
                    }
                    $this.addClass('active').siblings().removeClass('active');
                });
            return this;
        },
        /**
         * 表单
         */
        queryForm: function() {
            var v = $('#query_form').easyform(),_this = this;
            v.success = function (ef){//验证成功
                _this.params = $(ef.form).serializeObject(_this.params);
                _this.params.pageNO = 1;
                _this.querySearch(_this.params);
            };
            return this;
        },
        /**
         * 清除
         */
        clearForm: function() {
            let queryForm = $('#query_form'), _this = this;// 序列化表单内容
            queryForm[0].reset();
            this.params = {pageNO:1,pageSize:10};
            this.querySearch(this.params);
        },
        /**
         * 处理数据
         */
        processingData: function(data=[]) {
            data.map((item)=>{
                console.log(item)
            })
        },
        /**
         * 获取数据
         * @param 获取后的回调
         */
        getItems: function(cawllback) {
            this.ajaxHandle();
        },
        /**
         * ajax统一处理
         */
        ajaxHandle: function() {

        },
        /**
         * 统一提示语
         * @param msg 提示消息 string
         */
        tips: function(msg) {
            Util.alertMessage(msg);
        },
        /**
         * 获取选中数据
         */
        getTableCheckedData: function(){
            if(!this.checkedList) {
                return;
            }
            return this.checkedList();
        },
        /**
         * 导出
         */
        export: function(){
            // console.log(this.params)
            window.open(Util.exportParamFormat(API.exportEmployee,[]));
        },
        /*
        * 发布新闻
        * */
        addNews: function () {
            window.location.href = '#/news-add'
        }
    }

    handlers.init();

    var v = $('#query_form').easyform();
    v.success = function (ef){//验证成功
        handlers.params = $(ef.form).serializeObject(handlers.params);
        handlers.params.pageNO = 1;
        handlers.querySearch(handlers.params);
    }
    $('.resetBtn').on('click',function(){
        $('input[name=addressCode],input[name=companyCode]').val('');
        window.setTimeout(function(){
            handlers.params = {pageNO:1,pageSize:10};
            handlers.querySearch(handlers.params);
        },500);
    });

}